//
//  ZXLTopicDetailController.m
//  EPA
//
//  Created by 朱兴隆 on 16/4/22.
//  Copyright © 2016年 朱兴隆. All rights reserved.
//

#import "ZXLTopicDetailController.h"
#import "ZXLNetWorkRequestManager.h"
#import "ZXLTopicDetailCell.h"
#import "ZXLCommentlistModel.h"
#import "ZXLPostsInfoModel.h"
#import "ZXLUpCell.h"

#import "LORefresh.h"

@interface ZXLTopicDetailController ()
@property (nonatomic, strong) NSMutableArray *commentlistArray;
@property (nonatomic, strong) ZXLPostsInfoModel *postsInfo;



// header属性
@property (nonatomic, strong) LORefresh *headerRefresh;
@end

@implementation ZXLTopicDetailController
//- (void)dealloc {
//    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
//    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"cententOffset"];
//    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
//}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.commentlistArray = [NSMutableArray new];
//    self.postsInfo = [ZXLPostsInfoModel new];
    
    
    
    
    [self loadDataFromServerContentid:self.contentid];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZXLUpCell" bundle:nil] forCellReuseIdentifier:@"up"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZXLTopicDetailCell" bundle:nil] forCellReuseIdentifier:@"TopicDetail"];
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)loadDataFromServerContentid:(NSString *)contentid {
    [ZXLNetWorkRequestManager requestURL:[NSURL URLWithString:TOPICINFO_URL] Method:ZXL_POST Params:@{@"contentid":self.contentid,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (data) {
            
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSDictionary *dataDict = jsonDict[@"data"];
            
            
            // 楼主发言postsinfo
            NSDictionary *postsinfoDict = dataDict[@"postsinfo"];
          
            self.postsInfo = [ZXLPostsInfoModel new];
            
            [self.postsInfo setValuesForKeysWithDictionary:postsinfoDict];
            NSLog(@"%@",postsinfoDict);
            
            
            // 评论Comment
            NSDictionary *commentlistDict = dataDict[@"commentlist"][0];
            
            ZXLCommentlistModel *model = [[ZXLCommentlistModel alloc] init];
            [model setValuesForKeysWithDictionary:commentlistDict];
            [self.commentlistArray addObject:model];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            
        }
    }];
    // postsinfo
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.commentlistArray.count + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ZXLUpCell *cell = [tableView dequeueReusableCellWithIdentifier:@"up" forIndexPath:indexPath];
        cell.model = self.postsInfo;
        return cell;
    }
    
    ZXLTopicDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicDetail" forIndexPath:indexPath];
    
    cell.commentlist = self.commentlistArray[indexPath.row - 1];
    ZXLCommentlistModel *commentlist = self.commentlistArray[indexPath.row - 1];
    NSLog(@"%@",commentlist.userInfoModel.uname);
    // Configure the cell...
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.3)];
//
//    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 200, 40)];
//    nameLabel.backgroundColor = [UIColor greenColor];
//    nameLabel.text = self.postsInfo.userinfo.uname;
//    [headerView addSubview:nameLabel];
//
//
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:self.postsInfo.userinfo.icon]];
//    [headerView addSubview:imageView];
//
//
//    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 280, self.view.frame.size.width, 20)];
//    bottomView.backgroundColor = [UIColor grayColor];
//    [headerView addSubview:bottomView];
//
//    return headerView;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 300;
//}


@end
